import Vue from 'vue'
import store from '../store'
import Router from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import ForgetPassword from '../views/ForgetPassword.vue'
import ResetPassword from '../views/ResetPassword.vue'
import Search from '../views/Search.vue'
import Product from '../views/Product.vue'
import List from '../views/List.vue'
import UserCenter from '../views/UserCenter.vue'
import UserInfo from '../views/UserInfo.vue'
import OrderList from '../views/OrderList.vue'
import Category from '../views/Category.vue'
import Cart from '../views/Cart.vue'

Vue.use(Router)
const router = new Router({
  linkActiveClass: 'active',
  routes: [
    {
      path: '/',
      component: Home,
      redirect: '/home'
    },{
      path: '/cart',
      component: Cart,
      name: 'Cart',
      meta: {
        auth: true,
      }
    },{
      path: '/user-info',
      name: UserInfo.name,
      component: UserInfo,
      meta: {
        auth: true,
      }
    },{
      path: '/category',
      name: Category.name,
      component: Category,
    }, {
      path: '/order-list',
      name: OrderList.name,
      component: OrderList,
      meta: {
        auth: true,
      }
    },{
      path: '/list',
      component: List
    },  {
      name: 'user',
      path: '/user',
      component: UserCenter,
      meta: {
        auth: true,
      }
    }, {
      name: 'Home',
      path: '/home',
      component: Home
    }, {
      name: Search.name,
      path: '/search',
      component: Search
    }, {
      name: 'Login',
      path: '/login',
      component: Login
    }, {
      name: 'ForgetPassword',
      path: '/forget-password',
      component: ForgetPassword
    }, {
      name: 'ResetPassword',
      path: '/reset-password',
      component: ResetPassword
    }, {
      name: 'Register',
      path: '/register',
      component: Register
    }, {
      name: 'Product',
      path: '/product/:id',
      component: Product
    }
  ]
})

router.beforeEach((from,to,next)=>{
  console.log(from);
  if (from.meta.auth){
    if (store.state.isLogin){
      next();
    } else {
      next({
        path: '/login',
        query: {
          redirect: from.path
        }
      })
    }
  }else{
    next();
  }
})

export default router;
